package com.logisticsinquiry.android.ui.query

import android.content.DialogInterface
import android.content.Intent
import android.database.sqlite.SQLiteDatabase
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.snackbar.Snackbar
import com.logisticsinquiry.android.QueryApplication
import com.logisticsinquiry.android.R
import com.logisticsinquiry.android.TraceActivity
import com.logisticsinquiry.android.logic.database.AppDatabase
import com.logisticsinquiry.android.logic.entity.ShipperCodeResponseHistory
import kotlin.concurrent.thread


class ShipperAdapter(
    private val fragment: Fragment,
    private val shipperCodeResponseList: ArrayList<ShipperCodeResponseHistory>,
    val username: String
) :
    RecyclerView.Adapter<ShipperAdapter.ViewHolder>(){
    inner class ViewHolder(view: View): RecyclerView.ViewHolder(view){
        val LogisticCode : TextView = view.findViewById(R.id.LogisticCode)
        val shipperName: TextView = view.findViewById(R.id.shipperName)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.history_item, parent, false)
        val holder = ViewHolder(view)
        holder.itemView.setOnClickListener {
            val position = holder.adapterPosition
            val shipperCodeResponse = shipperCodeResponseList[position]
            val activity = fragment.activity
            thread {
                val shipperCodeResponseHistoryDao =
                    AppDatabase.getDatabase(QueryApplication.context)
                        .shipperCodeResponseHistoryDao()
                val shipperCodeResponseHistory = ShipperCodeResponseHistory(
                    shipperCodeResponse.logisticCode,
                    shipperCodeResponse.shipperName,
                    username
                )
                Log.v("数据库测试", shipperCodeResponseHistory.toString())
                if (shipperCodeResponseHistory.equals(
                        shipperCodeResponseHistoryDao.loadShipperCodeResponseHistoryByLogisticCode(
                            shipperCodeResponse.logisticCode,
                            username
                        )
                    )
                ) {
                    Log.v("数据库插入数据测试", "已存在相同单号，放弃插入记录")
                } else {
                    shipperCodeResponseHistory.id = shipperCodeResponseHistoryDao.insertShipperCodeResponseHistory(
                        shipperCodeResponseHistory
                    )
                }
                Log.v(
                    "数据库查询测试", shipperCodeResponseHistoryDao.loadAllShipperCodeResponseHistorys(
                        username
                    ).toString()
                )
            }
            val intent = Intent(parent.context, TraceActivity::class.java).apply {
                putExtra("logisticCode", shipperCodeResponse.logisticCode)
                putExtra("shipperName", shipperCodeResponse.shipperName)
            }
            fragment.startActivity(intent)
        }
        holder.itemView.setOnLongClickListener {
            val position = holder.adapterPosition
            val shipperCodeResponse = shipperCodeResponseList[position]
            val activity = fragment.activity
            thread {
                val shipperCodeResponseHistoryDao =
                    AppDatabase.getDatabase(QueryApplication.context)
                        .shipperCodeResponseHistoryDao()
                val shipperCodeResponseHistory = ShipperCodeResponseHistory(
                    shipperCodeResponse.logisticCode,
                    shipperCodeResponse.shipperName,
                    username
                )
                if (shipperCodeResponseHistory.equals(
                        shipperCodeResponseHistoryDao.loadShipperCodeResponseHistoryByLogisticCode(
                            shipperCodeResponse.logisticCode,
                            username
                        )
                    )
                ) {
                    shipperCodeResponseHistoryDao.deleteShipperCodeReponseHistoryBySomething(shipperCodeResponseHistory.logisticCode,shipperCodeResponseHistory.username)
                    Log.v("数据库删除测试", shipperCodeResponseHistoryDao.loadAllShipperCodeResponseHistorys(username).toString())
                } else {
                    Log.v("数据库删除测试", "不存在该历史记录，删除失败")
                }
                Snackbar.make(view,"Data deleted", Snackbar.LENGTH_SHORT)
                    .setAction("Undo"){
                        shipperCodeResponseHistory.id = shipperCodeResponseHistoryDao.insertShipperCodeResponseHistory(shipperCodeResponseHistory)
                        Toast.makeText(QueryApplication.context, "Data restored", Toast.LENGTH_SHORT).show()
                    }.show()


            }

            /*AlertDialog.Builder(QueryApplication.context)
                .setTitle("提示框")
                .setMessage("是否删除？")
                .setPositiveButton("yes", DialogInterface.OnClickListener { dialog, which ->
                    thread {
                        val shipperCodeResponseHistoryDao =
                            AppDatabase.getDatabase(QueryApplication.context)
                                .shipperCodeResponseHistoryDao()
                        val shipperCodeResponseHistory = ShipperCodeResponseHistory(
                            shipperCodeResponse.logisticCode,
                            shipperCodeResponse.shipperName,
                            username
                        )
                        Log.v("数据库删除测试", shipperCodeResponseHistory.toString())
                        if (shipperCodeResponseHistory.equals(
                                shipperCodeResponseHistoryDao.loadShipperCodeResponseHistoryByLogisticCode(
                                    shipperCodeResponse.logisticCode,
                                    username
                                )
                            )
                        ) {
                            shipperCodeResponseHistoryDao.deleteShipperCodeResponseHistory(
                                shipperCodeResponseHistory
                            )
                        } else {
                            Log.v("数据库删除测试", "不存在该历史记录，删除失败")
                        }
                    }
                })
                .setNegativeButton("no", null).show()*/
            true
        }
        return holder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val ShipperCodeResponse = shipperCodeResponseList[position]
        holder.LogisticCode.text = ShipperCodeResponse.logisticCode
        holder.shipperName.text = ShipperCodeResponse.shipperName
    }

    override fun getItemCount() = shipperCodeResponseList.size
}